package com.dbb.trade.service.product;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dbb.trade.dao.ColorDao;
import com.dbb.trade.dao.SizeDao;
import com.dbb.trade.dao.TagDao;
import com.dbb.trade.dao.company.ProductDao;
import com.dbb.trade.entity.Tag;
import com.dbb.trade.entity.product.Color;
import com.dbb.trade.entity.product.Product;
import com.dbb.trade.entity.product.Size;

@Component
@Transactional(readOnly = true)
public class ProductManager {

	private static Logger logger = LoggerFactory.getLogger(ProductManager.class);

	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private TagDao tagDao;
	
	@Autowired
	private ColorDao colorDao;
	
	@Autowired
	private SizeDao sizeDao;

	@Transactional(readOnly = false)
	public Product save(Product product) {
		logger.info("save {}", product);
		return productDao.save(product);
	}

	public List<Product> findAll() {
		return (List<Product>) productDao.findAll();
	}
	
	public Page<Product> findAll(Pageable pageable) {
		return productDao.findAll(pageable);
	}
	
	@Transactional(readOnly = false)
	public void delAll() {
		productDao.deleteAll();
	}
	
	public boolean exist(String ref) {
		return productDao.findByRef(ref) != null;
	}

	@Transactional(readOnly = false)
	public void addTag(String ref, String tagStr) {
		List<Tag> tags = Tag.getTags(tagStr);
		List<Tag> ts = new ArrayList<Tag>();
		for(Tag t : tags) {
			if(tagDao.findByName(t.getName()).size() == 0) {
				tagDao.save(t);
				ts.add(t);
			} else {
				ts.add(tagDao.findByName(t.getName()).get(0));
			}
		}
		Product pro = productDao.findByRef(ref);
		pro.setTags(ts);
		productDao.save(pro);
	}

	public List<Product> findByTags(String tagName) {
		List<Tag> tags = tagDao.findByNameLike(tagName);
		return productDao.findByTagsIn(tags);
		
	}

	public Product findByRef(String string) {
		return productDao.findByRef(string);
	}
	
	public Product setColors(Product pro, String ...colors) {
		Product product = null;
		if(pro.getId() == null) {
			productDao.save(pro);
			product = pro;
		} else {
			product = productDao.findOne(pro.getId());
		}
		List<Color> cs = new ArrayList<Color>();
		for(String c : colors) {
			if(colorDao.findByName(c) != null) {
				cs.add(colorDao.findByName(c));
			} else {
				cs.add(colorDao.save(new Color(c)));
			}
		}
		product.setColors(cs);
		return product;
	}
	
	public Product setSizes(Product pro, String ...sizes) {
		Product product = null;
		if(pro.getId() == null) {
			productDao.save(pro);
			product = pro;
		} else {
			product = productDao.findOne(pro.getId());
		}
		List<Size> cs = new ArrayList<Size>();
		for(String s : sizes) {
			if(sizeDao.findByName(s) != null) {
				cs.add(sizeDao.findByName(s));
			} else {
				cs.add(sizeDao.save(new Size(s)));
			}
		}
		product.setSizes(cs);
		return product;
	}

}
